package com.example.administrator.x2picturetransliteration.Home.Fragment;

import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.example.administrator.x2picturetransliteration.Base.BaseFragment;
import com.example.administrator.x2picturetransliteration.Base.BasePresenter;
import com.example.administrator.x2picturetransliteration.Bean.UserWorkListBean;
import com.example.administrator.x2picturetransliteration.Home.Adapter.FileFragmentAdapter;
import com.example.administrator.x2picturetransliteration.Home.Dialog.OkDialog;
import com.example.administrator.x2picturetransliteration.Home.Presenter.Fragment.FilePresenter;
import com.example.administrator.x2picturetransliteration.R;
import com.example.administrator.x2picturetransliteration.Util.ToastUtil;
import com.example.administrator.x2picturetransliteration.View.SwipeLayout.SwipeLayoutManager;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 文档
 * Created by Administrator on 2019/8/16.
 */

public class FileFragment extends BaseFragment implements OnRefreshListener,OnLoadMoreListener {

    @BindView(R.id.relative)View relative;

    @BindView(R.id.TitleText)TextView TitleText;
    @BindView(R.id.zIcon)ImageView zIcon;
    @BindView(R.id.text)TextView text;

    @BindView(R.id.sortView)View sortView;

    @BindView(R.id.deleteView)View deleteView;
    @BindView(R.id.listview)ListView listview;

    @BindView(R.id.searchEdit)TextView searchEdit; //搜索

    @BindView(R.id.noView)View noView; //暂无文档

    @BindView(R.id.qxImg)ImageView qxImg; //全选

    @BindView(R.id.smartRefreshLayout)SmartRefreshLayout smartRefreshLayout; //上拉下拉

    @BindView(R.id.noImg)ImageView noImg;
    @BindView(R.id.noText)TextView noText;

    private String listtype = "0"; //文档列表返回类型

    private FilePresenter mFilePresenter;

    private List<UserWorkListBean> mData; //列表数据
    private FileFragmentAdapter mFileFragmentAdapter; //列表适配器

    @Override
    protected int intiLayout() {
        return R.layout.home_fragment_file;
    }

    @Override
    public void operation(View view) {

        relative.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {

                relative.setFocusable(true);
                relative.setFocusableInTouchMode(true);
                relative.requestFocus();
                return false;
            }
        });

        TitleText.setText("全部");
        zIcon.setImageResource(R.mipmap.home_down);
        zIcon.setVisibility(View.VISIBLE);
        text.setText("编辑");

        mFilePresenter = new FilePresenter(getContext(),mCompositeSubscriptions);

        mData = new ArrayList<>();

        mFileFragmentAdapter = new FileFragmentAdapter(getContext(), mData,false, new FileFragmentAdapter.deleteCallback() {
            @Override
            public void delete(int position) {

                del(position);
            }

            @Override
            public void setName(int position, String name) {

                addwork(position,name);
            }
        });

        smartRefreshLayout.setOnRefreshListener(this);
        smartRefreshLayout.setOnLoadMoreListener(this);

        listview.setAdapter(mFileFragmentAdapter);

        if (mData.size()<=0)noView.setVisibility(View.VISIBLE);

        searchEdit.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                if (actionId == EditorInfo.IME_ACTION_SEARCH) {

                    if (deleteView.getVisibility()==View.VISIBLE){

                        new ToastUtil(FileFragment.this.getContext()).getToast(false,"处于编辑状态无法搜索").show();
                        return false;
                    }

                    noImg.setImageResource(R.drawable.home_kongsousuo);
                    noText.setText("无搜索后果");

                    list(true);

                    return true;
                }
                return false;
            }
        });

    }

    /** 上拉加载 */
    @Override
    public void onLoadMore(RefreshLayout refreshLayout) {

        list(false);
        refreshLayout.finishLoadMore(2000);
    }

    /** 下拉刷新 */
    @Override
    public void onRefresh(RefreshLayout refreshLayout) {

        list(true);
        refreshLayout.finishRefresh(2000);
    }

    /** 懒加载 */
    @Override
    public void LazyLoad() {

        list(true);
    }

    /** 排序选项 */
    @OnClick(R.id.TitleText)
    public void textClick(){

        sortView.setVisibility(sortView.getVisibility()==View.VISIBLE?View.GONE:View.VISIBLE);
    }

    /** 编辑 */
    @OnClick(R.id.text)
    public void deleteViewClick(){


        if (mData.size()==0){

            deleteView.setVisibility(View.GONE);
            new ToastUtil(getContext()).getToast(false,"没有文档").show();

        }else {

            deleteView.setVisibility(deleteView.getVisibility()==View.VISIBLE?View.GONE:View.VISIBLE);
        }

        qxImg.setImageResource(R.mipmap.home_check0);
        QxJudge = false;
        mFileFragmentAdapter.setDeleteVisible();
        SwipeLayoutManager.getInstance().closeCurrentLayout();
        SwipeLayoutManager.getInstance().clearCurrentLayout();
    }

    /** 全选 */
    private boolean QxJudge = false;
    @OnClick(R.id.qxView)
    public void qxImgClick(){

        if (QxJudge){

            qxImg.setImageResource(R.mipmap.home_check0);

        }else {

            qxImg.setImageResource(R.mipmap.home_check1);
        }

        QxJudge = !QxJudge;

        for (UserWorkListBean mUserWorkListBean:mData)mUserWorkListBean.setDeleteJudge(QxJudge);

        mFileFragmentAdapter.notifyDataSetChanged();
    }

    /** 删除 */
    @OnClick(R.id.deleteText)
    public void deleteTextClick(){

        new OkDialog(getContext(),"删除后不可恢复，您确定要删除？",new OkDialog.hd() {
            @Override
            public void hdff() {

                del(null);
            }
        }).show();
    }

    /** 查看全部结果 */
    @OnClick(R.id.CkText)
    public void CkTextClick(){

        TitleText.setText("全部");
        listtype = "0";
        sortView.setVisibility(View.GONE);
        list(true);
    }

    /** 查看识别结果 */
    @OnClick(R.id.SbText)
    public void SbTextClick(){

        TitleText.setText("识别");
        listtype = "1";
        sortView.setVisibility(View.GONE);
        list(true);
    }

    /** 查看翻译结果 */
    @OnClick(R.id.FyText)
    public void FyTextClick(){

        TitleText.setText("翻译");
        listtype = "3";
        sortView.setVisibility(View.GONE);
        list(true);
    }

    /** 文档列表 */
    private void list(final boolean Refresh){

        if (Refresh){

            mData.clear();
            mFileFragmentAdapter.notifyDataSetChanged();
        }

        Map<String,String> map = new HashMap<>();
        map.put("type",listtype);
        if (!"".equals(searchEdit.getText() + ""))map.put("name",searchEdit.getText() + "");

        mFilePresenter.list(Refresh, map, null,new BasePresenter.Callback() {
            @Override
            public void getData(Object data) {

                if (Refresh){

                    mData.clear();
                    mFileFragmentAdapter.notifyDataSetChanged();
                }


                List<UserWorkListBean> listdata = (List<UserWorkListBean>)data;

                mData.addAll(listdata);

                noView.setVisibility(mData.size()>0?View.GONE:View.VISIBLE);

                mFileFragmentAdapter.notifyDataSetChanged();

            }
        });

    }

    /** 删除文档 如果id不为null删除单条否则批量删除 */
    public void del(final Integer position){

        String ids = null;

        SwipeLayoutManager.getInstance().closeCurrentLayout();
        SwipeLayoutManager.getInstance().clearCurrentLayout();

        if (position!=null){

            ids = mData.get(position).getId();

        }else {

            for (UserWorkListBean mUserWorkListBean:mData){

                if (mUserWorkListBean.isDeleteJudge())
                    ids = ids==null ? mUserWorkListBean.getId() : ids +  "," + mUserWorkListBean.getId();
            }
        }

        if (ids==null)return;

        mFilePresenter.del(ids, new BasePresenter.Callback() {
            @Override
            public void getData(Object data) {

                if (position!=null){

                    mData.remove(position.intValue());

                }else {

                    Iterator<UserWorkListBean> it = mData.iterator();
                    while(it.hasNext()){

                        UserWorkListBean x = it.next();

                        if(x.isDeleteJudge()){
                            it.remove();
                        }
                    }

                }

                mFileFragmentAdapter.notifyDataSetChanged();

                if (mData.size()==0){

                    deleteView.setVisibility(View.GONE);
                    new ToastUtil(getContext()).getToast(false,"没有文档").show();
                    qxImg.setImageResource(R.mipmap.home_check0);
                    QxJudge = false;
                    mFileFragmentAdapter.setDeleteVisible();
                    SwipeLayoutManager.getInstance().closeCurrentLayout();
                    SwipeLayoutManager.getInstance().clearCurrentLayout();

                }
            }
        });
    }

    /** 重命名 */
    public void addwork(final int position, final String name){

        Map<String,String> httpdata = new HashMap<>();
        httpdata.put("id",mData.get(position).getId());
        httpdata.put("type",mData.get(position).getType());
        httpdata.put("name",name);

        mFilePresenter.addwork(httpdata, new BasePresenter.Callback() {
            @Override
            public void getData(Object data) {

                mData.get(position).setName(name);
                mFileFragmentAdapter.notifyDataSetChanged();
            }
        });
    }


}
